//
//  EditInfomationVC.m
//  KeleAppProject
//
//  Created by Cen Zhou on 2018/1/17.
//  Copyright © 2018年 Cen Zhou. All rights reserved.
//

#import "EditInfomationVC.h"

#import "TypeDefineTableViewCell.h"
#import "CellTintModel.h"
#import "CellTintViewModel.h"
#import "TextEditVC.h"
#import "BRDatePickerView.h"
#import "BRAddressPickerView.h"
#import "BRStringPickerView.h"

@interface EditInfomationVC ()<UITableViewDelegate,UITableViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic ,strong) UIImagePickerController * imagePicker;

@end

static NSString *editCell = @"editCell";

@implementation EditInfomationVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"编辑资料";
    self.view.backgroundColor = KLColor_C1;
    [self loadDataWithTint];
    [self loadTableView];
}

- (void)loadDataWithTint{
    self.dataArray = [CellTintViewModel returnEditInfoData];
}

// loadTableView
- (void)loadTableView{
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0,KLScreenW,340*HEIGHT) style:UITableViewStyleGrouped];
    tableView.scrollEnabled = NO;
    tableView.backgroundColor = KLColor_C1;
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.showsVerticalScrollIndicator = NO;
    tableView.showsHorizontalScrollIndicator = NO;
    tableView.sectionHeaderHeight = 0;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KLScreenW, 10*HEIGHT)];
    tableView.tableHeaderView = headerView;
    [self.view addSubview:tableView];
    self.tableView = tableView;
}

#pragma mark  ---- tableView 的代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    TypeDefineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:editCell];
    
    if (indexPath.row == 0) {
        if (!cell) {
            cell = [[TypeDefineTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:editCell type:DefineCellTypeImageAndInSecond];
        }
        // 各自的配置
        
    }else{
        if (!cell) {
            cell = [[TypeDefineTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:editCell type:DefineCellTypeTintAndInSecond];
        }
        cell.infoTint.text = @"未设置";
        cell.infoTint.textColor = KLColor_A1;
    }
    
    CellTintModel *model = self.dataArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = model;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50*HEIGHT;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50*HEIGHT;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        [self changeProfile];

    }else if (indexPath.row == 1 || indexPath.row == 2){
        [self enterTextEditWithIndexPath:indexPath];
    }else if (indexPath.row == 3){
        [self showSexSelector];
    }else if (indexPath.row ==4){
        [self showDateSelector];
    }else{
        [self showLocationSelector];
    }
}

#pragma mark --------------------------------------------选择相机-------------------------------------
- (void)changeProfile{
    
//    [UISplitViewController  ];
    [ASSheetViewController setSheetControllerWithTipTitle:nil message:nil itemsArray:@[@"从相册选择",@"拍照"] cancelTitle:@"取消" viewController:self handler:^(NSInteger index) {

        switch (index) {
            case 1:
                [self addImageFromCameraOrFromAlbum:UIImagePickerControllerSourceTypePhotoLibrary];
                break;
            case 2:
                [self addImageFromCameraOrFromAlbum:UIImagePickerControllerSourceTypeCamera];
                break;


            default:
                break;
        }
    }];
}

//添加图片或者拍照
- (void)addImageFromCameraOrFromAlbum:(UIImagePickerControllerSourceType)type{
    // 因为是controller 所以不能用self
    _imagePicker = [[UIImagePickerController alloc] init];
    _imagePicker.delegate = self;
    _imagePicker.sourceType = type;
    _imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    _imagePicker.allowsEditing = YES;
    _imagePicker.navigationBar.barTintColor = [UIColor whiteColor];
    _imagePicker.navigationBar.tintColor = [UIColor whiteColor];
    [self presentViewController:_imagePicker animated:YES completion:nil];
}
//得到拍下来的照片
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    // 得到照片信息
    // UIImage *images=[info objectForKey:UIImagePickerControllerOriginalImage];
    NSString* mediaType=[info objectForKey:UIImagePickerControllerMediaType];
    //得到图片是否为刚拍照的图片
    if([mediaType isEqualToString:@"public.image"])
    {
        UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
        // 把头像图片存到本地
        NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
        NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"image.jpg"]];   // 保存文件的名称
        NSData *imageData = UIImageJPEGRepresentation(image, 0.1);
        [imageData writeToFile:filePath atomically:YES];
        [_imagePicker dismissViewControllerAnimated:YES completion:nil];
        // 赋值
        //一个cell刷新
        NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0];
        TypeDefineTableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
        cell.cellImageView.image = [UIImage imageWithData:imageData];
//        [self.tableView reloadData];
    }
}

#pragma mark --------------------------------------------进入输入-------------------------------------
- (void)enterTextEditWithIndexPath:(NSIndexPath *)indexPath{
    TextEditVC *textEditVc = [[TextEditVC alloc]init];
    textEditVc.editType = indexPath.row==1?@"编辑昵称":@"编辑简介";
    [self.navigationController pushViewController:textEditVc animated:YES];
}

#pragma mark --------------------------------------------选择性别-------------------------------------

- (void)showSexSelector{
    NSArray *dataSources = @[@"男",@"女"];
    [BRStringPickerView showStringPickerWithTitle:@"性别" dataSource:dataSources defaultSelValue:dataSources[0] isAutoSelect:YES themeColor:KLColor_A1 resultBlock:^(id selectValue) {
        
    }];
}
#pragma mark --------------------------------------------选择日期-------------------------------------

- (void)showDateSelector{
    [BRDatePickerView showDatePickerWithTitle:@"日期" dateType:UIDatePickerModeDate defaultSelValue:@"" minDateStr:@"" maxDateStr:@"" isAutoSelect:YES themeColor:KLColor_A1 resultBlock:^(NSString *selectValue) {
        
    }];
}
#pragma mark --------------------------------------------选择地区-------------------------------------
- (void)showLocationSelector{
    [BRAddressPickerView showAddressPickerWithDefaultSelected:@[@0, @0, @0] isAutoSelect:YES themeColor:KLColor_A1 resultBlock:^(NSArray *selectAddressArr) {
        
    } cancelBlock:^{
        
    }];
//    [BRAddressPickerView showAddressPickerWithDefaultSelected:@[@0, @0, @0] isAutoSelect:YES resultBlock:^(NSArray *selectAddressArr) {
//        weakSelf.addressTF.text = [NSString stringWithFormat:@"%@%@%@", selectAddressArr[0], selectAddressArr[1], selectAddressArr[2]];
//    }];
}


- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
